﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.IO;
using System.Xml;

namespace LTM_Extra
{
    public class Util
    {
        public static string ConvertObjectToXml<T>(T objects)
        {
            string serializedObject;
            StringWriter writer = null;
            try
            {


                XmlSerializer serializer = new XmlSerializer(typeof(T));
                writer = new StringWriter();
                serializer.Serialize(writer, objects);
                serializedObject = writer.ToString();
            }
            finally
            {
                if (writer != null) writer.Close();
            }
            return serializedObject;


        }

        public static void SaveObjectTo<T>(T objects, string path)
        {
            string content=ConvertObjectToXml<T>(objects);

            File.WriteAllText(path, content, Encoding.UTF8);
 
        }


        public static T CreateObjectByXml<T>(String xmlFilePath)
        {
            
            StringReader stringReader = null;
            try
            {
                XmlSerializer serializer = new XmlSerializer(typeof(T));
                stringReader = new StringReader(File.ReadAllText(xmlFilePath));

                return (T)serializer.Deserialize(stringReader);
            }

            finally
            {
                if (stringReader != null) stringReader.Close();
            }

        }


    }
}
